/**
 * 
 */
package com.theunderminejournal.updater;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * 
 * 
 */
public class PropertiesUtil{

	private Properties properties;

	public final static String PROPERTY_FILE_NAME = "TUJ.properties";
	
	public final static String MARKET_DATA_FILE_KEY = "marketDataFile";
	
	public final static String TUJ_URL_KEY = "tujUrl";
	
	public final static String LAST_UPDATE_KEY = "lastUpdate";
	
	private Long periodSeconds = new Long((24 * 60 * 60 * 100));
	
	//private Long allowedUpdates = new Long(10);
	
	public PropertiesUtil() {
		properties = new Properties();
		loadProperties();
	}

	/**
	 * @return the properties
	 */
	public void loadProperties() {
		try {
			properties.load(new FileInputStream(PROPERTY_FILE_NAME));
		} catch (IOException e) {
		}
	}

	public void storeProperties(){
		try {
			properties.store(new FileOutputStream(PROPERTY_FILE_NAME), null);
		} catch (IOException e) {
		}
	}
	
	public String getMarketDataFile(){
		String marketDataFile = null;
		try {
			marketDataFile = (String) properties.get(MARKET_DATA_FILE_KEY);
		} catch (Exception e) {
		}
		return marketDataFile;
	}
	
	public void setMarketDataFile(String marketDataFile){
		properties.setProperty(MARKET_DATA_FILE_KEY, marketDataFile);
	}
	
	public String getTujUrl(){
		String tujUrl = null;
		try{
			tujUrl = (String)properties.get(TUJ_URL_KEY);
		}catch(Exception e){}
		return tujUrl;
	}
	
	public void setTujUrl(String tujUrl){
		properties.setProperty(TUJ_URL_KEY, tujUrl);
	}
	
	public void setLastUpdate(Long lastUpdate){		
		properties.setProperty(LAST_UPDATE_KEY, lastUpdate.toString());
	}
	
	public Long getLastUpdate(){
		Long lastUpdate = new Long(0);
		
		try{
			lastUpdate = new Long(properties.getProperty(LAST_UPDATE_KEY));
			
		}
		catch(Exception e){
			
		}
		
		return lastUpdate;
	}
	
	public Long getNextUpdate(){
		return getLastUpdate() + (periodSeconds);
	}

}
